package fi.foyt.cs.oauth.provider.data;

import org.restlet.ext.oauth.provider.data.AuthenticatedUser;
import org.restlet.ext.oauth.provider.data.Client;

import fi.foyt.cs.controller.OAuthController;

public class GAEClient implements Client {

  public GAEClient(String clientId, String clientSecret, String redirectUri, String name) {
    this.clientId = clientId;
    this.redirectUri = redirectUri;
    this.clientSecret = clientSecret;
    this.applicationName = name;
  }

  public boolean containsUser(String userId) {
    OAuthController oAuthController = new OAuthController();
    fi.foyt.cs.persistence.domainmodel.oauth.AuthenticatedUser authenticatedUser = oAuthController.findAuthenticatedUserByUserId(userId);
    return authenticatedUser != null;
  }

  public AuthenticatedUser createUser(String id) {
    OAuthController oAuthController = new OAuthController();
    fi.foyt.cs.persistence.domainmodel.oauth.AuthenticatedUser authenticatedUser = oAuthController.createAuthenticatedUser(id, "");
    AuthenticatedUser user = new GAEAuthenticatedUser(authenticatedUser.getUserId());
    return user;
  }

  public AuthenticatedUser findUser(String id) {
    OAuthController oAuthController = new OAuthController();
    fi.foyt.cs.persistence.domainmodel.oauth.AuthenticatedUser authenticatedUser = oAuthController.findAuthenticatedUserByUserId(id);
    if (authenticatedUser != null) {
      return new GAEAuthenticatedUser(authenticatedUser.getUserId());
    }
    
    return null;
  }

  public void revokeUser(String id) {
    OAuthController oAuthController = new OAuthController();
    fi.foyt.cs.persistence.domainmodel.oauth.AuthenticatedUser authenticatedUser = oAuthController.findAuthenticatedUserByUserId(id);
    oAuthController.deleteAuthenticatedUser(authenticatedUser);
  }

  public String getClientSecret() {
    return clientSecret;
  }

  public String getRedirectUri() {
    return redirectUri;
  }

  public String getClientId() {
    return clientId;
  }

  public String getApplicationName() {
    return applicationName;
  }
  
  private final String clientId;
  private String clientSecret;
  private String redirectUri;
  private String applicationName;
}
